package yogiro.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import yogiro.dao.NoticeDAO;
import yogiro.to.NoticeCommentTO;
import yogiro.to.NoticeListTO;
import yogiro.to.NoticeTO;
import yogiro.to.PagingTO;
import yogiro.util.Utilities;

public class NoticeService {
	private NoticeDAO noticeDAO;
	
	
	public void setNoticeDAO(NoticeDAO noticeDAO){
		this.noticeDAO = noticeDAO;
	}
	public NoticeListTO getBoardListByPage1(int page,String txt) throws SQLException{
		HashMap map=new HashMap();
		map.put("rownum", Utilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("noticeTitle", txt);
		ArrayList<NoticeTO> list = noticeDAO.selectNoticeAllList1(map);
		for(NoticeTO nto:list){
			int i=nto.getNoticeNo();
			nto.setCommentCount(noticeDAO.selectCommentCount(i));
			System.out.println(nto.getCommentCount());
			nto.setNoticeWritedate(Utilities.changeDayFormat(nto.getNoticeWritedate()));
		}
		int totalContent = noticeDAO.byTitleCount();
		PagingTO pagingTO = new PagingTO(totalContent, page);
		NoticeListTO lto = new NoticeListTO(list, pagingTO);
		return  lto;
	}

	public NoticeListTO getBoardListByPage2(int page,String txt) throws SQLException{
		HashMap map=new HashMap();
		map.put("rownum", Utilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("noticeWriter", txt);
		ArrayList<NoticeTO> list = noticeDAO.selectNoticeAllList2(map);
		for(NoticeTO nto:list){
			int i=nto.getNoticeNo();
			nto.setCommentCount(noticeDAO.selectCommentCount(i));
			System.out.println(nto.getCommentCount());
			nto.setNoticeWritedate(Utilities.changeDayFormat(nto.getNoticeWritedate()));
		}
		int totalContent = noticeDAO.byWriterCount();
		PagingTO pagingTO = new PagingTO(totalContent, page);
		NoticeListTO lto = new NoticeListTO(list, pagingTO);
		return  lto;
	}
	public NoticeListTO getBoardListByPageDate(int page,String star,String end) throws SQLException{
		HashMap map=new HashMap();
		System.out.println(page+""+star+""+end);
		map.put("rownum", Utilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("str", star);
		map.put("end", end);
		ArrayList<NoticeTO> list = noticeDAO.selectNoticeDate(map);
		
		
		System.out.println("서비스처음");
		for(NoticeTO nto:list){
			
			int i=nto.getNoticeNo();
			nto.setCommentCount(noticeDAO.selectCommentCount(i));
			System.out.println(nto.getCommentCount());
			
			
		}
			System.out.println("오냐구얌얌얌");
			HashMap map1=new HashMap();
			map1.put("str", star);
			map1.put("end", end);
			int count = noticeDAO.byDate(map1);
		
			PagingTO pagingTO = new PagingTO(count, page);
			NoticeListTO lto = new NoticeListTO(list, pagingTO);
			System.out.println("lto까징?");
			return  lto;
		}
		
	public NoticeListTO getBoardListByPage(int page) throws SQLException{
		HashMap map=new HashMap();
		map.put("rownum", Utilities.CONTENT_PER_PAGE);
		map.put("page", page);
		ArrayList<NoticeTO> list = noticeDAO.selectNoticeAllList(map);
		
		for(NoticeTO nto:list){
			nto.setNext(nto.getNoticeNo());
			int i=nto.getNoticeNo();
			nto.setCommentCount(noticeDAO.selectCommentCount(i));
			System.out.println(nto.getCommentCount());
			
			
			nto.setNoticeWritedate(Utilities.changeDayFormat(nto.getNoticeWritedate()));
			
		}
		
		int totalContent = noticeDAO.selectTotalContent();
		PagingTO pagingTO = new PagingTO(totalContent, page);
		NoticeListTO lto = new NoticeListTO(list, pagingTO);
		return  lto;
	}
	public NoticeTO getContentByNO(int no) throws SQLException{
		noticeDAO.updateViewCount(no);
		NoticeTO nto = noticeDAO.selectContentByNO(no);
		nto.setNoticeContent(Utilities.changeContentForTextArea(nto.getNoticeContent()));
		System.out.println("콘텐츠를 찍자"+nto.getNoticeContent());
			
		nto.setNoticeWritedate(Utilities.changeDayTimeFormat(nto.getNoticeWritedate()));
		return nto;
	}
	public void writeContent(NoticeTO nto) throws SQLException{
		int no = noticeDAO.seqNo();
		String writeDate = Utilities.getNow();
		String c = Utilities.changeContentForDB(nto.getNoticeContent());
		nto.setNoticeNo(no);
		nto.setNoticeWritedate(writeDate);
		nto.setNoticeContent(c);
		noticeDAO.insertNotice(nto);
		nto.setNoticeWritedate(Utilities.changeDayTimeFormat(writeDate));
	}
	public void deleteContentByNO(int no) throws SQLException{
		noticeDAO.deleteContentByNO(no);
	}
	public NoticeTO getContentByNoForForm(int no) throws SQLException{
		NoticeTO nto = noticeDAO.selectContentByNO(no);
		nto.setNoticeContent(Utilities.changeContentForTextArea(nto.getNoticeContent()));
		return nto;
	}
	public void modifyContent(NoticeTO nto) throws SQLException{
		nto.setNoticeContent(Utilities.changeContentForDB(nto.getNoticeContent()));
		nto.setNoticeWritedate(Utilities.getNow());
		noticeDAO.updateContent(nto); 
		nto.setNoticeWritedate(Utilities.changeDayTimeFormat(nto.getNoticeWritedate()));
	}
	
		public void deleteComment(int commentNo, int noticeNo) throws SQLException{
			noticeDAO.deleteComment(commentNo, noticeNo);
		
	}
	
		public NoticeCommentTO selectByNo(int commentNo) throws SQLException{
		 NoticeCommentTO cto = noticeDAO.selectByNo(commentNo);
		 return cto;
	}
		public ArrayList<NoticeCommentTO> selectCommentByNO(int notice_no)throws SQLException{
			ArrayList<NoticeCommentTO> list=null;
			System.out.println("서비스에서");
			list=noticeDAO.selectCommentByNO(notice_no);
			return list;
		}
		public ArrayList<NoticeTO> selectByTitle(String title)throws SQLException{
			ArrayList<NoticeTO> list=null;
			return list=noticeDAO.selectByTitle(title);
		}
		public ArrayList<NoticeTO> selectByWriter(String writer)throws SQLException{
			ArrayList<NoticeTO> list=null;
			return list=noticeDAO.selectByWriter(writer);
		}
		public int selectCommentCount(int noticeNo)throws SQLException{
			return noticeDAO.selectCommentCount(noticeNo);
		}
		public int byNextNO(int noticeNo)throws SQLException{
			return noticeDAO.byNextNO(noticeNo);
		}
		public int byMax()throws SQLException{
			return noticeDAO.byMax();
		}
		public int byPreNO(int noticeNo)throws SQLException{
			return noticeDAO.byPreNO(noticeNo);
		}
		public int byMin()throws SQLException{
			return noticeDAO.byMin();
		}
}
